﻿using MahApps.Metro.Controls;
using System.ComponentModel;
using System.Windows;

namespace VM.Start.Core
{
    public class ModuleViewBase : MetroWindow
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ModuleViewBase()
        {
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.IsMinButtonEnabled = false;
            Loaded += OnLoaded;
        }

        #region  属性
        /// <summary>
        /// ui对应的module
        /// </summary>
        public ModuleBase ModuleBase { get; set; }

        /// <summary>
        /// 备份 取消的时候还原
        /// </summary>
        private ModuleBase ModuleBaseBack;

        public bool IsClosed { get; set; } = true;
        private bool _isVisible; // 用于标记窗口的可见状态
        #endregion
        #region 方法
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            if (ModuleBase == null) return;
            DataContext = ModuleBase;
            this.Title = ModuleBase.ModuleParam.ModuleName;
            ModuleBase.Loaded();
        }

        private void Cancel()
        {
            //Project prj = Solution.Ins.GetProjectById(ModuleBaseBack.ModuleParam.ProjectID);
            //prj.RecoverModuleObj(ModuleBaseBack);//还原
            this.Close();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (ModuleBase != null && ModuleBase.ClosedView)
            {
                ModuleBase.ModuleView = null;
                return;
            }
            e.Cancel = true;  // cancels the window close
            IsClosed = true;
            this.Hide();      // Programmatically hides the window
        }

        protected override void OnClosed(EventArgs e)
        {
            _isVisible = false; // 关闭时设置为不可见
            base.OnClosed(e);
        }

        // 显示窗口的方法
        public void ShowModule()
        {
            IsClosed = false; // 设置 IsClosed 为 false

            if (!this.IsVisible) // 检查窗口是否未显示
            {
                this.Topmost = true;  // 设置窗口为最上层
                this.ShowDialog();          // 显示窗口
            }
            else
            {
                this.Topmost = true;  // 确保窗口为最上层
                this.Activate();      // 激活窗口
            }

            //// 防止窗口永久保持最上层，如果需要，可以重置
            //this.Topmost = false;

        }

        #endregion


    }
}
